package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.base.BaseResult;
import com.ls.base.BaseResultUtil;
import com.ls.controller.model.channel.EditChannelModel;
import com.ls.controller.model.channel.QueryChannelModel;
import com.ls.controller.model.commService.AddCommServiceModel;
import com.ls.controller.model.commService.EditCommServiceModel;
import com.ls.controller.model.commService.QueryCommServiceModel;
import com.ls.entity.CommChannel;
import com.ls.entity.CommService;
import com.ls.entity.SysUser;
import com.ls.exception.SysException;
import com.ls.mapper.CommServiceMapper;
import com.ls.service.ICommServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.ls.base.BaseResultUtil.SQL_ERROR;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2019-12-05
 */
@Service
public class CommServiceServiceImpl extends ServiceImpl<CommServiceMapper, CommService> implements ICommServiceService {

    @Autowired
    CommServiceMapper mapper;

    @Override
    public BaseResult add(AddCommServiceModel model, SysUser currentUser) {
        boolean checkFlag = checkServiceNameExist(model.getServiceName());
        if (checkFlag) {
            return BaseResultUtil.error("业务名称已存在");
        }
        CommService commService = new CommService();
        BeanUtils.copyProperties(model, commService);
        commService.setCreateTime(LocalDateTime.now());
        commService.setCreateUserId(currentUser.getUserId());
        commService.setUpdateTime(LocalDateTime.now());
        commService.setUpdateUserId(currentUser.getUserId());
        int result = mapper.insert(commService);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    @Override
    public boolean checkServiceNameExist(String serviceName) {
        if (StringUtils.isBlank(serviceName)) {
            throw new SysException("checkServiceNameExist 需要参数");
        }
        CommService commService = mapper.selectOne(new QueryWrapper<CommService>().eq("service_name", serviceName));
        if (commService == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public BaseResult edit(EditCommServiceModel model, SysUser currentUser) {
        CommService commService = mapper.selectById(model.getServiceId());
        if (commService == null) {
            throw new SysException("checkChannelNameExist 需要参数");
        }
        String chanNameNew = model.getServiceName();
        if (!commService.getServiceName().equals(chanNameNew)) {
            boolean commChannelCheck = checkServiceNameExist(chanNameNew);
            if (commChannelCheck) {
                throw new SysException(chanNameNew + " 在库里已经存在");
            }
        }
        BeanUtils.copyProperties(model, commService);
        commService.setUpdateTime(LocalDateTime.now());
        commService.setUpdateUserId(currentUser.getUserId());
        int result = mapper.updateById(commService);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    @Override
    public BaseResult listByPage(QueryCommServiceModel model, SysUser currentUser) {
        QueryWrapper<CommService> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(model.getServiceName())) {
            wrapper.like("service_name", model.getServiceName());
        }
        if (model.getOpenStatus() != null) {
            wrapper.eq("open_status", model.getOpenStatus());
        }
        IPage<CommService> pageData = mapper.selectPage(new Page<>(model.getPageNum(), model.getPageSize()), wrapper);
        return BaseResultUtil.success(pageData);
    }

}
